﻿namespace ConsoleAppArray
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");

            List<Device> list = new List<Device>();
            list.Add(
                new Device()
                {
                    Id = 101,
                    Name = "1号设备",
                    Type = "生产设备",
                    Result = 99,
                }
            );
            list.Add(
                new Device()
                {
                    Id = 102,
                    Name = "2号设备",
                    Type = "生产设备",
                    Result = 60,
                }
            );
            list.Add(
                new Device()
                {
                    Id = 103,
                    Name = "3号设备",
                    Type = "测试设备",
                    Result = 98,
                }
            );
            list.Add(
                new Device()
                {
                    Id = 104,
                    Name = "4号设备",
                    Type = "测试设备",
                    Result = 70,
                }
            );
            list.Add(
                new Device()
                {
                    Id = 201,
                    Name = "5号生产设备",
                    Type = "生产设备",
                    Result = 100,
                }
            );
            list.Add(
                new Device()
                {
                    Id = 202,
                    Name = "6号测试设备",
                    Type = "测试设备",
                    Result = 89,
                }
            );
            list.Add(
                new Device()
                {
                    Id = 203,
                    Name = "7号测试设备",
                    Type = "测试设备",
                    Result = 98,
                }
            );
            list.Add(
                new Device()
                {
                    Id = 204,
                    Name = "8号测试设备",
                    Type = "测试设备",
                    Result = 95,
                }
            );
            // 使用LINQ查询方法

            foreach (var item in list.ToList())
            {
                list.Remove(item);
                Console.WriteLine("已删除：" + item.Name);
            }

            // 1. 选择所有设备名称
            var deviceNames = list.Select(x => x.Name);

            var nameList = list.Select(x => x.Name).ToList();

            nameList.Add("11号设备");

            var deviceIds = list.Select(x => x.Id);

            var dtos = list.Select(x => new DeviceDto { Id = x.Id, Result = x.Result });

            // 输出结果
            Console.WriteLine("\n所有设备名称:");
            foreach (var name in deviceNames)
            {
                Console.WriteLine(name);
            }

            // 过滤生产设备
            var productionDevices = list.Where(device => device.Type == "生产设备");
            var productionDevices2 = list.Where(ProductDevice);

            Console.WriteLine("\n生产设备:");
            foreach (var device in productionDevices)
            {
                Console.WriteLine($"ID: {device.Id}, 名称: {device.Name}, 结果: {device.Result}");
            }

            Console.WriteLine("\n生产设备:");
            foreach (var device in productionDevices2)
            {
                Console.WriteLine($"ID: {device.Id}, 名称: {device.Name}, 结果: {device.Result}");
            }

            //Where
            var device90s = list.Where(device => device.Result >= 90);
            Console.WriteLine("\n生产合格设备:");
            foreach (var device in device90s)
            {
                Console.WriteLine($"ID: {device.Id}, 名称: {device.Name}, 结果: {device.Result}");
            }

            var pro90Devices = list.Where(x => x.Type == "生产设备" && x.Result >= 90);

            //First/FirstOrDefault

            //var dev1 = list.First(x => x.Id == 101);
            var dev2 = list.FirstOrDefault(x => x.Id == 101);
            //var dev1 = list.First(x => x.Id == 0);  //报错
            //var dev2 = list.FirstOrDefault(x => x.Id == 0);
            //var dev1 = list.First(x => x.Id > 200);
            //var dev2 = list.FirstOrDefault(x => x.Id > 200);
            Console.WriteLine("\n单设备查找:");
            // Console.WriteLine($"ID: {dev1.Id}, 名称: {dev1.Name}, 结果: {dev1.Result}");
            //Console.WriteLine($"ID: {dev2.Id}, 名称: {dev2.Name}, 结果: {dev2.Result}");

            Console.WriteLine("\n All方法测试");
            var result = list.All(x => x.Result > 90);
            Console.WriteLine($"result: {result}");

            Console.WriteLine("\n Any方法测试");
            var result2 = list.Any(x => x.Result > 90);
            Console.WriteLine($"result: {result2}");

            Console.WriteLine("\nCount方法示例");
            var count = list.Count();
            var count2 = list.Count(device => device.Type == "生产设备");
            Console.WriteLine($"count: {count}");
            Console.WriteLine($"count2: {count2}");
        }

        static bool ProductDevice(Device device)
        {
            return device.Type == "生产设备";
        }
    }

    class DeviceDto
    {
        /// <summary>
        /// Id
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 测试结果
        /// </summary>
        public int Result { get; set; }
    }

    /// <summary>
    /// 设备类
    /// </summary>
    class Device
    {
        /// <summary>
        /// Id
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 设备类型
        /// </summary>
        public string Type { get; set; }

        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 测试结果
        /// </summary>
        public int Result { get; set; }
    }
}
